package com.mproject.adapter;

import java.security.Guard;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

import com.mproject.activity.R;
import com.mproject.activity.project.Project_Schedule_Month;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class GridCellAdapter extends BaseAdapter implements OnClickListener
{
	private static final String tag = "GridCellAdapter";
	private final Context _context;

	private final List<String> list;
	private static final int DAY_OFFSET = 1;
	private final String[] weekdays = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
	private final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	private final int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	private final int month, year;
	private int daysInMonth, prevMonthDays;
	private int currentDayOfMonth;
	private int currentWeekDay;
	private Button gridcell;
	private TextView events_num;
	private Button current_gridcell;
	private TextView num_events_per_day;
	//	private final HashMap eventsPerMonthMap;
	//private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");

	// Days in Current Month
	public GridCellAdapter(Context context, int textViewResourceId, int month, int year, boolean isFirstTime)
	{
		super();
		this._context = context;
		this.list = new ArrayList<String>();
		this.month = month;
		this.year = year;

		Log.d(tag, "==> Passed in Date FOR Month: " + month + " " + "Year: " + year);
		Calendar calendar = Calendar.getInstance();

		if (isFirstTime == true) {
			setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
		}
		else {
			setCurrentDayOfMonth(1);
		}
		setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
		Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
		Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
		Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());

		// Print Month
		printMonth(month, year);

		// Find Number of Events
		//eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
	}
	private String getMonthAsString(int i)
	{
		return months[i];
	}

	private String getWeekDayAsString(int i)
	{
		return weekdays[i];
	}

	private int getNumberOfDaysOfMonth(int i)
	{
		return daysOfMonth[i];
	}

	public String getItem(int position)
	{
		return list.get(position);
	}


	public int getCount()
	{
		return list.size();
	}

	/**
	 * Prints Month
	 * 
	 * @param mm
	 * @param yy
	 */
	private void printMonth(int mm, int yy)
	{
		Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
		// The number of days to leave blank at
		// the start of this month.
		int trailingSpaces = 0;
		int leadSpaces = 0;
		int daysInPrevMonth = 0;
		int prevMonth = 0;
		int prevYear = 0;
		int nextMonth = 0;
		int nextYear = 0;

		int currentMonth = mm - 1;
		String currentMonthName = getMonthAsString(currentMonth);
		daysInMonth = getNumberOfDaysOfMonth(currentMonth);

		Log.d(tag, "Current Month: " + " " + currentMonthName + " having " + daysInMonth + " days.");

		// Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
		GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
		Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

		if (currentMonth == 11)
		{
			prevMonth = currentMonth - 1;
			daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
			nextMonth = 0;
			prevYear = yy;
			nextYear = yy + 1;
			Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:" + prevMonth + " NextMonth: " + nextMonth + " NextYear: " + nextYear);
		}
		else if (currentMonth == 0)
		{
			prevMonth = 11;
			prevYear = yy - 1;
			nextYear = yy;
			daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
			nextMonth = 1;
			Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:" + prevMonth + " NextMonth: " + nextMonth + " NextYear: " + nextYear);
		}
		else
		{
			prevMonth = currentMonth - 1;
			nextMonth = currentMonth + 1;
			nextYear = yy;
			prevYear = yy;
			daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);

			Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:" + prevMonth + " NextMonth: " + nextMonth + " NextYear: " + nextYear);
		}

		// Compute how much to leave before before the first day of the
		// month.
		// getDay() returns 0 for Sunday.
		int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
		trailingSpaces = currentWeekDay;

		Log.d(tag, "Week Day:" + currentWeekDay + " is " + getWeekDayAsString(currentWeekDay));
		Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
		Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

		if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 2)
		{
			++daysInMonth;
		}
		if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 3)
			daysInPrevMonth++;

		// Trailing Month days
		for (int i = 0; i < trailingSpaces; i++)
		{
			Log.d(tag, "PREV MONTH:= " + prevMonth + " => " + getMonthAsString(prevMonth) + " " + String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i));
			list.add(String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" + getMonthAsString(prevMonth) + "-" + prevYear);
		}

		// Current Month Days
		for (int i = 1; i <= daysInMonth; i++)
		{
			Log.d(currentMonthName, String.valueOf(i) + " " + getMonthAsString(currentMonth) + " " + yy);
			if (i == getCurrentDayOfMonth())
			{
				list.add(String.valueOf(i) + "-BLUE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
			}
			else
			{
				list.add(String.valueOf(i) + "-WHITE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
			}
		}

		// Leading Month days
		for (int i = 0; i < list.size() % 7; i++)
		{
			Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
			list.add(String.valueOf(i + 1) + "-GREY" + "-" + getMonthAsString(nextMonth) + "-" + nextYear);
		}
	}

	/**
	 * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve
	 * ALL entries from a SQLite database for that month. Iterate over the
	 * List of All entries, and get the dateCreated, which is converted into
	 * day.
	 * 
	 * @param year
	 * @param month
	 * @return
	 */


	public long getItemId(int position)
	{
		return position;
	}


	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		if (row == null)
		{
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.calendar_day_gridcell, parent, false);
		}

		// Get a reference to the Day gridcell
		gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
		gridcell.setOnClickListener(this);


		// ACCOUNT FOR SPACING

		Log.d(tag, "Current Day: " + getCurrentDayOfMonth());

		String[] day_color = list.get(position).split("-");
		String theday = day_color[0];
		String themonth = day_color[2];
		String theyear = day_color[3];

		int month=0;
		for(int i=0;i<months.length;i++)
			if(months[i].equals(themonth))
				month=i+1;
		/*events_num=(TextView)row.findViewById(R.id.event_dot);*/
		//date="2012-04-12";
		/*Log.d(tag,"checking events for the day "+theyear+"-"+((month>10)?month:"0"+month)+"-"+((theday.length()>1)?theday:"0"+theday));
		FeedBackDataSource ds=new FeedBackDataSource(_context);
		int size=ds.getEventList(theyear+"-"+((month>10)?month:"0"+month)+"-"+((theday.length()>1)?theday:"0"+theday)).size();
		Log.d(tag,size+" event number");
		if(size>0)
		{
			events_num.setVisibility(View.VISIBLE);
		}
		else
		{
			events_num.setVisibility(View.INVISIBLE);
		}*/



		/*if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null))
		{
			if (eventsPerMonthMap.containsKey(theday))
			{
				num_events_per_day = (TextView) row.findViewById(R.id.num_events_per_day);
				Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
				num_events_per_day.setText(numEvents.toString());
			}
		}*/

		// Set the Day GridCell
		gridcell.setText(theday);
		gridcell.setTag(theday + "-" + themonth + "-" + theyear);

		Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-" + theyear);


		if (day_color[1].equals("GREY"))
		{
			gridcell.setTextColor(Color.LTGRAY);
		}
		if (day_color[1].equals("WHITE"))
		{
			gridcell.setTextColor(Color.BLACK);
		}
		if (day_color[1].equals("BLUE"))
		{
			gridcell.setTextColor(Color.BLACK);
			gridcell.setBackgroundResource(R.drawable.calendar_bg_blue);
			current_gridcell=gridcell;
		}


		return row;
	}

	public void onClick(View view)
	{				
		String date_month_year = (String) view.getTag();
		String[] day_color = date_month_year.split("-");
		String theday = day_color[0];
		String themonth = day_color[1];
		String theyear = day_color[2];

		if (themonth.equals("January"))
			themonth = "00";
		else if (themonth.equals("February"))
			themonth = "01";
		else if (themonth.equals("March"))
			themonth = "02";
		else if (themonth.equals("April"))
			themonth = "03";
		else if (themonth.equals("May"))
			themonth = "04";
		else if (themonth.equals("June"))
			themonth = "05";
		else if (themonth.equals("July"))
			themonth = "06";
		else if (themonth.equals("August"))
			themonth = "07";
		else if (themonth.equals("September"))
			themonth = "08";
		else if (themonth.equals("October"))
			themonth = "09";
		else if (themonth.equals("November"))
			themonth = "10";
		else if (themonth.equals("December"))
			themonth = "11";

		int month=Integer.parseInt(themonth)+1;
		String mnth=(month>9?month+"":"0"+month);
		/*currentdate.setText(dateFormatter.format(dateTemplate1, new Date(Integer.parseInt(theyear)-1900,Integer.parseInt(themonth),Integer.parseInt(theday))));
		currentday.setText(dateFormatter.format(dateTemplate2, new Date(Integer.parseInt(theyear)-1900,Integer.parseInt(themonth),Integer.parseInt(theday))));
		currentmonthnew.setText(dateFormatter.format(dateTemplate3,new Date(Integer.parseInt(theyear)-1900,Integer.parseInt(themonth),Integer.parseInt(theday))));*/
		current_gridcell.setBackgroundResource(R.drawable.calendar_bg_transparent);
		current_gridcell=(Button)view.findViewById(R.id.calendar_day_gridcell);
		current_gridcell.setBackgroundResource(R.drawable.calendar_bg_blue);
		Log.e(tag,"date other than current is selected "+theyear+"-"+themonth+"-"+((theday.length()>1)?theday:"0"+theday));
		/*updateDisplay(theyear+"-"+mnth+"-"+((theday.length()>1)?theday:"0"+theday));*/
		//"2012-04-12"
	}

	public int getCurrentDayOfMonth()
	{
		return currentDayOfMonth;
	}

	private void setCurrentDayOfMonth(int currentDayOfMonth)
	{
		this.currentDayOfMonth = currentDayOfMonth;
	}
	public void setCurrentWeekDay(int currentWeekDay)
	{
		this.currentWeekDay = currentWeekDay;
	}
	public int getCurrentWeekDay()
	{
		return currentWeekDay;
	}



}
